const { dialog, ipcMain } = require('electron')
let fs = require('fs')

ipcMain.handle('on-openFile-event',  (e) => {
    fs.open('./public/overview.json', 'a+', (err, fd) =>{
        if (err){
            console.log(err)
        }
        fs.close(fd)
    })
    dialog.showOpenDialog({ properties: ['openFile', 'multiSelections'] }).then(result =>{

        fs.readFile(result.filePaths[0],function (err,data){
            if (err) {
                return console.error(err);
            }
            // console.log("异步读取: " + data.toString('utf8'));
            let jsonData=eval("("+data.toString('utf-8')+")");
            let msg = "所属题库："+jsonData.database+"\n试卷名称："+jsonData.name+"\n考试日期："+jsonData.exam_date+"\n考试时长："+jsonData.duration+"分钟\n题量："+jsonData.question_volume+"\n确认添加吗？"
            // console.log(json)
            dialog.showMessageBox({
                message: msg,
                type: 'info',
                title: '检索到以下信息'
            }).then((result)=>{
                if (result.response === 0){
                    let fileName = ""+Date.now()+".json"
                    fs.readFile('./public/overview.json',function (err,data) {
                        if (err) {
                            return console.error(err);
                        }
                        let overviewData = null
                        if (data.toString() != ""){
                            overviewData=eval("("+data.toString('utf-8')+")");
                            for (let i=0;i< overviewData.dbs.length;i++) {
                                if(overviewData.dbs[i].name === jsonData.name && overviewData.dbs[i].database === jsonData.database){
                                    dialog.showMessageBox({
                                        message: "系统中已存在相同试卷",
                                        type: 'error',
                                        title: '系统信息'
                                    })
                                    return console.error("系统中已存在相同试卷");
                                }
                            }
                        }
                        else {
                            let str = "{\"dbs\": []}"
                            overviewData = eval("("+str+")")
                        }
                        let overview = "{\"database\": \""+jsonData.database+"\",\"name\": \""+jsonData.name+"\",\"exam_date\": \""+jsonData.exam_date+"\",\"duration\": "+jsonData.duration+",\"question_volume\": "+jsonData.question_volume+",\"file_name\": \"" +fileName+"\"}"
                        overviewData.dbs.unshift(eval("("+overview+")"))
                        fs.writeFile('./public/overview.json', JSON.stringify(overviewData),  function(err) {
                            if (err) {
                                dialog.showMessageBox({
                                    message: err,
                                    type: 'error',
                                    title: '系统信息'
                                })
                                return console.error(err);
                            }
                        })
                        fs.writeFile('./public/'+fileName, JSON.stringify(jsonData),  function(err) {
                            if (err) {
                                dialog.showMessageBox({
                                    message: err,
                                    type: 'error',
                                    title: '系统信息'
                                })
                                return console.error(err);
                            }
                            dialog.showMessageBox({
                                message: "添加成功",
                                type: 'info',
                                title: '系统信息'
                            })
                        });

                    })



                }
            })
        })


    })
})